#!/bin/bash
usage1='Usage: ./goinit.sh project_name    generate CI/CD files'
usage2='       ./goinit.sh -d              delete all CI/CD files' 
attention='project_name should not be "demoxyz", "jinquan", "jinquan7"'
if [ $# -ne 1 ]; then
    echo "$usage1"
    echo "$usage2"
    echo $attention
elif [ $1 == "-d" ]; then
    echo "delete files: Dockerfile, entrypoint.sh, jenkinsfile, readme.md"
    rm -rf Dockerfile entrypoint.sh jenkinsfile readme.md
else
project=$1

cat << EOF > Dockerfile
FROM registry.supor.com/infra/alpine:3.18 as container
MAINTAINER jinquan jinquan7@foxmail.com
LABEL Description="golang common service"
USER root
COPY entrypoint.sh /home/jinquan/entrypoint.sh
RUN  chmod +x /home/jinquan/entrypoint.sh;      chown jinquan:safe -R /home/jinquan;
USER jinquan
WORKDIR /home/jinquan
ENV app_routine=demo
COPY demo /home/jinquan/app/demo
COPY etc /home/jinquan/app/etc/
RUN  tree -acs /home/jinquan
EXPOSE 8888
ENTRYPOINT ["/home/jinquan/entrypoint.sh"]
EOF

sed -i "s/demo/$project/g" Dockerfile

echo "[add] Dockerfile"

cat << EOF > entrypoint.sh
#!/bin/bash
tree -acs /home/jinquan/
exec /home/jinquan/app/\$app_routine
EOF

echo "[add] entrypoint.sh"

mkdir -p helm
mkdir -p etc 
mkdir -p cmd  
echo "[add] folder: helm,etc,cmd"

cat << EOF > jenkinsfile
pipeline{
    agent any
    
    environment {
        compile_app = 'demo'     // <--
        artifact = "demo"        // <--
        tag_prefix = "jks04lux"  // <--
    }
    
    stages {
        stage("Checkout") {
            steps {
                git url: "https://gitee.com/jinquan711/demo.git",   // <--
                    credentialsId: "gitpull_token-jks04lux-gitee",  // <--
                    branch: "dev"                                   // <--
            }
        }
        
        stage('Compiler Environment'){
            steps{
                script {
                    sh '''
                      whoami
                      pwd
                      export PATH=\$PATH:/usr/local/musl/bin:/usr/local/go/bin
                      
                      go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct
                      go env -w GO111MODULE=on
                    '''
                }
            }
        }
        
        stage('Compile'){
            steps{
                script {
                    sh '''
                      rm -rf go.mod
                      export PATH=\$PATH:/usr/local/musl/bin:/usr/local/go/bin
                      
                      go mod init main
                      go mod tidy
                      go mod vendor
                      
                      CC=musl-gcc go build -tags musl -ldflags="-extldflags --static" -o \$compile_app cmd/*.go
                    '''
                }
            }
        }

        stage('Build image'){
            steps{
                script {
                    sh '''
                      ls -la .
                      sudo /usr/local/bin/buildctl build \\
                      --frontend=dockerfile.v0 \\
                      --local context=. \\
                      --local dockerfile=. \\
                      --output type=image,name=registry.supor.com/infra/\$artifact:\$tag_prefix-\$BUILD_NUMBER,push=true
                    '''
                }
            }
        }
    }
}
EOF

sed -i "s/demo/$project/g" jenkinsfile
echo "[add] jenkinsfile"


cat << EOF > readme.md
## 编译
\`\`\`
go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct
go env -w GO111MODULE=on

go mod init cmd/main
go mod tidy
go mod vendor
go build -o demo cmd/*.go
\`\`\`
## 启动服务
\`\`\`
nohup ./demo > demo.log 2>&1 &
\`\`\`

## 访问服务
\`\`\`
curl -XPOST http://127.0.0.1:8888/api/v1/xxx -H 'Content-Type: application/json' -d '{"key":"value"}'
\`\`\`
EOF

sed -i "s/demo/$project/g" readme.md
echo "[add] readme.md"

fi
